import goodsApi from '../../api/goods.api';

const state={
  data:[],
  newGoods: [],
  goodDetail: []
};

const getters={
  newGoods: (state: any) => {
    return state.newGoods;
  },

  goodDetail: (state: any) => {
    return state.goodDetail;
  }
};

const actions={
  async getNewGoods({commit}:any,params: any={}){
    params.filter="is_new||$eq||1";
    params.fields="name,goods_brief,retail_price,market_price,list_pic_url";
    const data=await goodsApi.getGoods(params);
    commit('setNewGoods', data);
  },

  async getGoodDetail({commit}:any,id: string){
    const params={
      filter:`id||$eq||${id}`,
      join:['goodsAttribute','goodsSpecification','product','couponGoods','couponGoods.coupon','goodsAttribute.attribute','goodsSpecification.specification']
    };
    const data=await goodsApi.getGoods(params);
    commit('setGoodDetail', data);
  }
};

const mutations={
  setNewGoods(state: any, data: any){
    state.newGoods = data;
  },

  setGoodDetail(state: any, data: any){
    state.goodDetail = data;
  }
};

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}